#include <stdlib.h>
#include "list.h"

void list_init(list_t **list, int i)
{
    if (list == NULL)
        return;

    /* Allocating memory for the head node. */
    *list = (list_t *) malloc(sizeof(list_t));
    if (*list == NULL)
        return;

    /* Assigning the i-value that represents a word index. */
    (*list)->i = i;
    (*list)->next = NULL;
}

void list_free(list_t **list)
{
    if (list == NULL || *list == NULL)
        return;

    /* Recursive deleting. */
    list_free(&(*list)->next);
    free(*list);
    *list = NULL;
}

void list_add(list_t **list, int i)
{
    list_t *new_i;
    list_init(&new_i, i);
    /* Adds a new element as the head and links the old elements. */
    new_i->next = *list;
    *list = new_i;
}

int list_iter(list_t *list)
{
    static list_t *current = NULL;
    int i;

    if (list != NULL)
        current = list;

    /* If isn't the ending of the list returns a default value. */
    if (current != NULL) {
         i = current->i;
         current = current->next;
    } else
        i = -1;
    return i;
}
